package com.lovo.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.commom.vo.ResultVO;
import com.lovo.sys.bean.AdminBean;
import com.lovo.sys.bean.UserBean;
import com.lovo.sys.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @GetMapping("add")
    public ResultVO add(AdminBean admin) {
        adminService.add(admin);
        ResultVO resultVO = new ResultVO();
        resultVO.setTag(0);
        resultVO.setJson("添加管理员成功");
        return resultVO;
    }

    @RequestMapping("updatePwd/{id}/{newPwd}")
    public ResultVO updatePwd(@PathVariable("id") int id, @PathVariable("newPwd") String newPwd) {
        adminService.updatePwd(id, newPwd);
        ResultVO resultVO = new ResultVO();
        resultVO.setTag(0);
        resultVO.setJson("修改密码成功");
        return resultVO;
    }

    @GetMapping("del/{id}")
    public ResultVO del(@PathVariable("id") int id) {
        adminService.del(id);
        ResultVO resultVO = new ResultVO();
        resultVO.setTag(0);
        resultVO.setJson("删除管理员成功");
        return resultVO;
    }

    @GetMapping("findAll/{pageNO}")
    public IPage<AdminBean> findAll(@PathVariable("pageNO") int pageNO) {
        return adminService.findAll(pageNO);
    }


    @GetMapping("login")
    public AdminBean login(String pwd, String userName, HttpServletRequest request) {
        AdminBean login = adminService.login(pwd, userName);

        request.getSession().setAttribute("loginUser", login);

        return login;
    }

}
